/*
 * PreferencesDialog.java
 *
 * Created on October 19, 2008, 3:08 PM
 */

package sk.yw.azetclient.gui;

import sk.yw.azetclient.Main;

/**
 *
 * @author  error216
 */
public class PreferencesDialog extends javax.swing.JDialog {

    /** Creates new form PreferencesDialog */
    public PreferencesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        
        messageThreadStorageIntervalSpinner.setValue(
                Long.parseLong(Main.getProperty(
                "azetclient.messageThreadStorage.interval")));
        buddyListRefreshIntervalSpinner.setValue(
                Long.parseLong(Main.getProperty(
                "azetclient.buddyList.interval")));
        messageRetrievalIntervalSpinner.setValue(
                Long.parseLong(Main.getProperty(
                "azetclient.message.interval")));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        messageThreadStorageIntervalSpinner = new javax.swing.JSpinner();
        buddyListRefreshIntervalSpinner = new javax.swing.JSpinner();
        messageRetrievalIntervalSpinner = new javax.swing.JSpinner();
        messageThreadStorageIntervalLabel = new javax.swing.JLabel();
        buddyListRefreshIntervalLabel = new javax.swing.JLabel();
        messageRetrievalIntervalLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(Main.getText("preferencesDialog.title"));

        messageThreadStorageIntervalSpinner.setModel(new javax.swing.SpinnerNumberModel(Long.valueOf(12000L), Long.valueOf(1000L), null, Long.valueOf(500L)));

        buddyListRefreshIntervalSpinner.setModel(new javax.swing.SpinnerNumberModel(Long.valueOf(3000L), Long.valueOf(1000L), null, Long.valueOf(500L)));

        messageRetrievalIntervalSpinner.setModel(new javax.swing.SpinnerNumberModel(Long.valueOf(3000L), Long.valueOf(1000L), null, Long.valueOf(500L)));

        messageThreadStorageIntervalLabel.setText(Main.getText("preferencesDialog.messageThreadStorageInterval"));

        buddyListRefreshIntervalLabel.setText(Main.getText("preferencesDialog.buddyListRefreshInterval"));

        messageRetrievalIntervalLabel.setText(Main.getText("preferencesDialog.messageRetrievalInterval"));

        okButton.setText(Main.getText("preferencesDialog.ok"));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(Main.getText("preferencesDialog.cancel"));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        applyButton.setText(Main.getText("preferencesDialog.apply"));
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(messageThreadStorageIntervalLabel)
                    .addComponent(buddyListRefreshIntervalLabel)
                    .addComponent(messageRetrievalIntervalLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(messageThreadStorageIntervalSpinner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buddyListRefreshIntervalSpinner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(messageRetrievalIntervalSpinner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addGap(5, 5, 5)
                .addComponent(applyButton)
                .addGap(12, 12, 12))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(messageThreadStorageIntervalLabel)
                    .addComponent(messageThreadStorageIntervalSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buddyListRefreshIntervalLabel)
                    .addComponent(buddyListRefreshIntervalSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(messageRetrievalIntervalLabel)
                    .addComponent(messageRetrievalIntervalSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(applyButton)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    applyPreferences();
    dispose();
}//GEN-LAST:event_okButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
    applyPreferences();
}//GEN-LAST:event_applyButtonActionPerformed
    
    private void applyPreferences() {
        Main.setPreferenceValue(
                "azetclient.messageThreadStorage.interval",
                String.valueOf(messageThreadStorageIntervalSpinner.getValue()));
        Main.setPreferenceValue(
                "azetclient.buddyList.interval",
                String.valueOf(buddyListRefreshIntervalSpinner.getValue()));
        Main.setPreferenceValue(
                "azetclient.message.interval",
                String.valueOf(messageRetrievalIntervalSpinner.getValue()));
        Main.applyPreferences();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PreferencesDialog dialog = new PreferencesDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JLabel buddyListRefreshIntervalLabel;
    private javax.swing.JSpinner buddyListRefreshIntervalSpinner;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel messageRetrievalIntervalLabel;
    private javax.swing.JSpinner messageRetrievalIntervalSpinner;
    private javax.swing.JLabel messageThreadStorageIntervalLabel;
    private javax.swing.JSpinner messageThreadStorageIntervalSpinner;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

}
